<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class lbpicController extends Controller
{
     // 分类添加页
    public function getAdd()
    {
        // dd('niao'); 
        return view('admin.lbpic.add');
    }
    public function postInsert(Request $request)
    {
       $this->validate($request, [
        'lbname' => 'required|min:2|max:10',
        'lbpic' => 'required',
        ],[
            'lbname.required'=>'轮播名字不能为空',
            'lbname.min'=>'轮播名字太短',
            'lbname.max'=>'轮播名字太长',
            'lbpic.required'=>'轮播图片不能为空',
        ]);

        $data = $request->only(['lbname']);
        if($request->hasFile('lbpic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('lbpic')->getClientOriginalExtension();
            // dd($su);jpg png jpeg gif tiff
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://lamp154.cn/test/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('lbpic')->move('./uploads/', $name.'.'.$su);
            // echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            $data['lbpic']= '/uploads/'.$name.'.'.$su;
        }
        // dd($data);
        // var_dump($data['use_id']);die;
        // var_dump($data);die;

        // 执行数据操作
        $res = DB::table('lbpic')->insert($data);
        // var_dump($res);die;
       
        if($res){

            return redirect('/lbpic/index')->with('success','轮播添加成功');
        }else{
            return back()->with('error','轮播添加失败');
        } 
    }
     public function getIndex(Request $request)
    {       
        // dd($request->all());

        

         $cates = DB::table('lbpic')->get();
       
       
        return view('admin.lbpic.index',['cates'=>$cates]);
    }

    public function getEdit($id) 
    {
        // dd($id);
        $lbpic = DB::table('lbpic')->where('id',$id)->first();
        // dd($lbpic);
        
        // echo '2222';
        return view('admin.lbpic.edit',['lbpic'=>$lbpic]);
    }

     public function postUpdate(Request $request)
    {

        $this->validate($request, [
        'lbname' => 'required|min:2|max:10',
       
        ],[
            'lbname.required'=>'轮播名不能为空',
            'lbname.min'=>'轮播名太短',
            'lbname.max'=>'轮播名太长',
            
        ]);
        // dd($request->all());
        $id = $request->input('id');
        $use_id = $request->input('use_id');
        $data = $request->only(['lbname']);
        // $data = $request->only(['lbname']);

        if($request->hasFile('lbpic')){
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('lbpic')->getClientOriginalExtension();
            // dd($su);jpg png jpeg gif tiff
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://lamp154.cn/test/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('lbpic')->move('./uploads/', $name.'.'.$su);
            // echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            $data['lbpic']= '/uploads/'.$name.'.'.$su;
        }

         $res = DB::table('lbpic')->where('id',$id)->update($data);

        if($res){
            return redirect('/lbpic/index')->with('success','店铺名称修改成功');
        }else{
            return back()->with('error','轮播名称修改失败');
        } 
        
   } 

   // 删除分类
    public function postDelete(Request $request)
            {
                // 获取id
                $id = $request->input('id');
               
                // 执行删除
                $res = DB::table('lbpic')->where('id',$id)->delete();
                if($res){
                    // 删除成功
                    echo 2;

                }else{
                    // 删除失败
                    echo 1;
                }
            }
}  
